package com.gxg.demo10.mydemo11;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.socks.library.KLog;

/**
 * 作者：Administrator on 2018/3/9 09:08
 * 邮箱：android_gaoxuge@163.com
 */
public class ToggleButton extends View {

    private Paint bgPaint;
    private int bgRadio;
    private Paint circlePait;

    private float toggleLeft;
    private float toggleMaxLeft;


    public ToggleButton(Context context) {
        this(context,null);
        init();
    }


    public ToggleButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
        init();
    }

    public ToggleButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void init() {
        //背景的画笔
        bgPaint = new Paint();
        bgPaint.setColor(Color.GREEN);
        //抗锯齿，画图质量提高
        bgPaint.setAntiAlias(true);
        bgRadio = 20;

        //化圆形的笔
        circlePait = new Paint();
        circlePait.setColor(Color.RED);
        //抗锯齿，画图质量提高
        circlePait.setAntiAlias(true);


    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasureSize(300, widthMeasureSpec);
        int height = getMeasureSize(150, heightMeasureSpec);

        KLog.e("sss  width  "+width);
        KLog.e("sss  height  "+height);
        toggleMaxLeft = getWidth() - (getHeight()-getTop())/2;
        toggleLeft = (getHeight()-getTop())/2;
        setMeasuredDimension(width,height);
    }

    private int getMeasureSize(int defaultSize, int measureSpec) {
        int mysize = defaultSize;

        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        KLog.e("sss  "+mode +"   "+size);
        switch (mode){
            case MeasureSpec.UNSPECIFIED://如果没有指定大小，就设置为默认大小
                KLog.e("sss "+"UNSPECIFIED");
                mysize = defaultSize;
                break;
            case MeasureSpec.AT_MOST://如果测量模式是最大取值为size
                KLog.e("sss "+"AT_MOST");
                //我们将大小取最大值,你也可以取其他值
                mysize = defaultSize;
                break;
            case MeasureSpec.EXACTLY://如果是固定的大小，那就不要去改变它
                KLog.e("sss "+"EXACTLY");
                mysize = size;
                break;
        }

        return mysize;
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //float left, float top, float right, float bottom,
        // float rx, x方向上的圆角半径。
        // float ry,y方向上的圆角半径。

        if(toggleLeft<0){
            toggleLeft = (getHeight()-getTop())/2;
        }
        if(toggleLeft>toggleMaxLeft){
            toggleLeft = toggleMaxLeft;
        }
        RectF r1= new RectF();                         //Rect对象
        r1.left=0;                               //左边
        r1.top=getTop();                                  //上边
        r1.right= getRight()-getLeft();                            //右边
        r1.bottom=getBottom()-getTop();
        KLog.e("sss " +getWidth()+"   "+getHeight());
        KLog.e("sss   "+getLeft()+"  "+getTop()+"   "+getRight()+"   "+getBottom());

        canvas.drawRoundRect(r1,bgRadio,bgRadio,bgPaint);

        canvas.drawCircle(toggleLeft,getBottom()/2,(getHeight()-getTop())/2,circlePait);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        KLog.e("sss   "+x+"   "+y);
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                toggleLeft =event.getX() ;
                if( toggleLeft<0){
                    toggleLeft=0;
                }
                KLog.e("sss  toggleLeft   "+toggleLeft);
                break;
            case MotionEvent.ACTION_UP:
                if(event.getX()>getWidth()/2){
                    toggleLeft = toggleMaxLeft;
                }else{
                    toggleLeft = (getHeight()-getTop())/2;
                }
                break;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
